package com.me.controller;

import com.me.annotation.Admin;
import com.me.annotation.Authorize;
import com.me.model.Item;
import com.me.model.User;
import com.me.model.normal.NormalItem;
import com.me.model.normal.NormalUser;
import com.me.services.IItemService;
import com.me.services.ServiceManager;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collection;

/**
 * @author 吕旺平
 * @date 2019/10/20 15:57:10
 * @description
 */

@Authorize
public class ItemController extends Controller{
	IItemService<User,Item> iService = ServiceManager.getItemService();
	
	public ItemController(HttpServletRequest req, HttpServletResponse resp) {
		super(req, resp);
	}
	
	@Override
	public String excute() {
		return getItemPage();
	}
	
	public String getItemPage(){
		if (u == null) return "/user/login.jsp";
		
		//初始化
		String itemIdStr = getParam("itemId");
		int itemId = itemIdStr != null ? Integer.parseInt(itemIdStr) : 0;
		int nextId = Integer.parseInt(getParam("nextId"));
		if (getSessionAttr("itemCount") == null) {
			setSessionAttr("itemCount", iService.getItemCount());
		}
		
		iService.initial(u);
		
		
		Item item = iService.getItemFromUser(u,itemId);
		
		storeItem(u,item);
		
		if (nextId >= iService.getItemCount()){
			return checkOver(itemId);
		}else{
			//给出下一道题
			Item nextItem = iService.getItemFromUser(u,nextId);
			setReqAttr("item", nextItem);
			return "/exam/item.jsp";
		}
	
	}
	
	@Admin
	public String add(){
		//添加题目 String title, String[] contents, int score, String answer, boolean multiple
		iService.addItem(new NormalItem(
				getParam("title"),
				getParams("contents"),
				getIntegerParam("score"),
				getParam("answer"),
				getBooleanParam("multiple")
		));
		return redirectToAction("manage");
	}
	
	@Admin
	public String manage(){
		Collection<Item> items = iService.getAll();
		setReqAttr("items",items);
		return "/admin/ItemManage.jsp";
	}
	
	@Admin
	public String edit(){
		int id = getIntegerParam("id");
		setReqAttr("item", iService.getItem(id));
		return "/admin/ItemEdit.jsp";
	}

	@Admin
	public String update() {
		NormalItem item = new NormalItem(
				getParam("title"),
				getParams("contents"),
				getIntegerParam("score"),
				getParam("answer"),
				getBooleanParam("multiple")
		);

		item.setId(getIntegerParam("id"));

		if (!iService.update(item)) {
			setRedirectParam("msg", "更新题目失败，未找到相应的题目");
		}

		return redirectToAction("manage");
	}
	
	@Admin
	public String delete(){
		int id = getIntegerParam("id");

		if (!iService.delete(id)) {
			setRedirectParam("msg","删除题目失败",true);
		}
		return redirectToAction("manage");
	}
	
	private void storeItem(User u,Item item){
		if (getParam("choose") != null) {
			iService.record(u, item, sumAll(getParams("choose")));
		}
		
	}

	@Admin
	private String checkOver(int itemId){
		String confirmStr = getParam("confirm");
		if (itemId == iService.getItemCount() - 1){
			if (confirmStr.equals("true")){
				StringBuilder atnSb = null;
				for (Item i : iService.getAllFromUser(u)) {
					if (i.getChooseId() == -1) {//有题没做
						if (atnSb == null){
							atnSb = new StringBuilder("您的第");
						}
						atnSb.append(i.getId() + 1);
						atnSb.append(",");
					}
				}
				if (atnSb != null){
					atnSb.deleteCharAt(atnSb.length() - 1);
					atnSb.append("题没有做哦，但如果真的很想交就点击下方确定吧");
				}else{
					atnSb = new StringBuilder("恭喜！题目已经做完，确认无误就点击下方确定吧");
				}
				setReqAttr("attention",atnSb.toString());
				setReqAttr("item", iService.getItemFromUser(u,itemId));
				return "/exam/item.jsp";
			}else if(!confirmStr.equals("false")){
				u.setConfirm(true);
				return "./exam/result.jsp";
			}
		}
		return "/error.jsp";
	}
	
	private int sumAll(String[] strs) {
		int res = 0;
		for (String s : strs) {
			res += Integer.parseInt(s);
		}
		return res;
	}
}
